package util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class DBUtils {

    private static DataSource ds;
    static {
        try {
            Properties pro = new Properties();
            pro.load(DBUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            ds=DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() {
        Connection conn=null;
        try {
            conn = ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;

    }
    public  static  void close(ResultSet rs,PreparedStatement stat,Connection conn){
            try {
                   if (rs!=null) {
                    rs.close();}
                    if (stat!=null){
                        stat.close();}
                    if (conn!=null){
                        conn.close();}
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }




}
